home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / ScriptX / Documentation / Code Examples from Docs / compguid / threads / musical.sx < prev   
Encoding:
Text File  |  1996-05-21  |  2.6 KB  |  75 lines  |  [TEXT/ttxt]

  1. -- <<<-
  2. module ThreadTest2 uses ScriptX end
  3. in module ThreadTest2
  4.  
  5. -- threads demonstration
  6. -- use this version here, a slight revision of the one in the book,
  7. --
  8. -- cool stuff to try
  9. -- use the Listener window to play with the threads
  10. -- start them, stop them, restart them, kill them
  11. -- change their relative priorities and other properties
  12. -- change the size of thePipe and its other properties
  13. -- create more threads and activate them too
  14. -- see how many normal threads it takes to thrash the system
  15. -- see how many high-priority threads it takes to thrash the system
  16.  
  17. object myWindow (Window)
  18.         boundary:(new Rect x2:300 y2:50), fill:whiteBrush
  19.         settings x:20, y:40
  20. end
  21. show myWindow
  22. -- create text string global
  23. object myTextBox (TextEdit)
  24.         boundary:(myWindow.boundary), target:("musician" as Text)
  25.         fill:whiteBrush, stroke:blackBrush
  26. end
  27. setDefaultAttr myTextBox @alignment @center
  28. append myWindow myTextBox
  29. show myWindow
  30.  
  31. -- now set up the threads
  32. function classical thePipe -> (
  33.                 local composerList := #("Beethoven","Bach","Mozart","Haydn",
  34.                         "Stravinsky","Mahler","Debussy","Ravel","Sibelius","Lutoslawski")
  35.                 repeat while true do (
  36.                         write thePipe (getAny composerList)
  37.                         threadYield()
  38.                         )
  39. )
  40.  
  41. function heavyMetal thePipe -> (
  42.                 local heavyMetalList := #("Aerosmith","Bon Jovi","Motley Crue",
  43.                         "Megadeath","Twisted Sister","Judas Priest","AC/DC","Metallica")
  44.                 repeat while true do (
  45.                         write thePipe (getAny heavyMetalList)
  46.                         threadYield()
  47.                         )
  48. )
  49.  
  50. function listenTo thePipe -> -- this is the reading thread
  51.     repeat while true do \
  52.         myTextBox.target := (read thePipe) as Text
  53. object myPipe (PipeClass) maxSize:3 end
  54.  
  55. -- create the three threads.  They will start active.
  56. global classic := new Thread func:classical arg:myPipe priority:@user
  57. global metal := new Thread func:heavyMetal arg:myPipe priority:@user
  58. global listen := new Thread func:listenTo arg:myPipe priority:@user
  59.                 
  60.                 
  61.                 
  62. -- Here's another one to add to the system
  63. function playJazz thePipe -> (
  64.                 local composerList := #("Armstrong","Ellington","Parker","Hawkins",
  65.                         "Young","Monk","Mingus","Coltrane","Davis","Carla Bley")
  66.                 repeat while true do (
  67.                         write thePipe (getAny composerList)
  68.                         threadYield()
  69.                         )
  70. )
  71. global jazz:= new Thread func:playjazz arg:myPipe priority:@user
  72.  
  73.  
  74. -->>>
  75.